package day17_Inet.demo02_UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//UDP接收数据:
//创建接收端的Scoket对象(DatagramSocket)
//创建数据包,用于接收数据
//调用Scoket对象(DatagramSocket)receice()方法接收数据
//解析数据包,在控制台显示 getData()
//关闭接收端
public class Receive_UDP {
    public static void main(String[] args) throws Exception {
        /*
        1.创建接收端的Scoket对象(DatagramSocket)
        new DatagramSocket(int port)
        2.创建数据包,用于接收数据
        new byte[1024]
        new DatagramPacket(byte[] buf, int length)
        3.调用Scoket对象(DatagramSocket)方法接收数据
        receice()
        4.解析数据包,在控制台显示
        getData()
        getLength()
        5.关闭接收端
        close()
         */
        DatagramSocket ds = new DatagramSocket(8888);
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
        ds.receive(dp);
        byte[] data = dp.getData();
        System.out.println(new String(data,0,data.length));
        int length = dp.getLength();
        System.out.println(length);
        ds.close();
    }
}
